﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using Cow.Handler;
using Cow.Module;
using Cow.CommandLine;
using System.IO;
using Cow.CommandLineText;

namespace Cow
{
    class Program
    {
        private static readonly HeadingInfo _headingInfo = new HeadingInfo("Cow", "1.0");

        private sealed class Options : CommandLineOptionsBase
        {
            #region Standard Option Attribute
            [Option("i", "input", Required = true, HelpText = "输入文件路径(xlst)")]
            public String Input { get; set; }

            [Option("o", "output", Required = true, HelpText = "输出文件目录")]
            public String Output { get; set; }

            [Option("t", "target", Required = true, HelpText = "输出类型(Entity|Map|Viewmodel|Schema|Repository|Create|Edit|List|View)")]
            public CodeTarget Target { get; set; }

            [Option("n", "namespace", DefaultValue = "", HelpText = "命名空间")]
            public String Namespace { get; set; }
            #endregion

            #region Help Screen
            [HelpOption]
            public string GetUsage()
            {
                return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
            }

            /*
            [HelpOption]
            public string GetUsage()
            {
                return HelpText.AutoBuild(this, delegate(HelpText current) {
                    if (this.LastPostParsingState.Errors.Count > 0)
                    {
                        var errors = current.RenderParsingErrorsText(this, 2); // indent with two spaces
                        if (!string.IsNullOrEmpty(errors))
                        {
                            current.AddPreOptionsLine(string.Concat(Environment.NewLine, "ERROR(S):"));
                            current.AddPreOptionsLine(errors);
                        }
                    }
                });
            }
            */

            /*
            [HelpOption]
            public string GetUsage()
            {
                var help = new HelpText { Heading = Program._headingInfo,
                    Copyright = new CopyrightInfo("Giacomo Stelluti Scala", 2005, 2012),
                    AdditionalNewLineAfterOption = true,
                    AddDashesToOption = true
                };
                this.HandleParsingErrorsInHelp(help);
                help.AddPreOptionsLine("This is free software. You may redistribute copies of it under the terms of");
                help.AddPreOptionsLine("the MIT License <http://www.opensource.org/licenses/mit-license.php>.");
                help.AddPreOptionsLine("Usage: Cow -iMyData.in -oMyData.out -tEntity -nsMyNamespace");
                help.AddOptions(this);
                return help;
            }

            private void HandleParsingErrorsInHelp(HelpText help)
            {
                if (this.LastPostParsingState.Errors.Count > 0)
                {
                    var errors = help.RenderParsingErrorsText(this, 2); // indent with two spaces
                    if (!string.IsNullOrEmpty(errors))
                    {
                        help.AddPreOptionsLine(string.Concat(Environment.NewLine, "ERROR(S):"));
                        help.AddPreOptionsLine(errors);
                    }
                }
            }
            */
            #endregion
        }

        static void Main(string[] args)
        {
            //            args = new String[] { @"-iD:\Practice\ed-Link.xlsx", @"-oD:\Practice\MyTest", "-tEntity","-nMytest.kk" };
            var options = new Options();
            var parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
            if (!parser.ParseArguments(args, options))
                Environment.Exit(1);
            var codeFactory = new CodeFactory();
            
            codeFactory.CreateCode(options.Input, options.Namespace, options.Output, options.Target);
            Environment.Exit(0);
        }
    }
}
